import http from '@/utils/http'
import type { ApiResponse } from '@/types/api'
import type { MessageListResponse, Message } from '@/types/message'
import { generateMockMessages, getMockMessageDetail } from '@/views/userViews/message/mockData'

// 判断是否使用模拟数据（开发环境）
const useMockData = import.meta.env.MODE === 'development'

// 获取用户消息列表
export const getUserMessages = (params: {
  page?: number;
  pageSize?: number;
  type?: number;
  status?: number;
}): Promise<ApiResponse<MessageListResponse>> => {
  // 开发环境使用模拟数据
  if (useMockData) {
    return new Promise(resolve => {
      setTimeout(() => {
        const mockData = generateMockMessages({
          page: params.page || 1,
          pageSize: params.pageSize || 10,
          type: params.type,
          status: params.status
        })
        resolve({
          code: 200,
          message: 'success',
          data: mockData,
          status: 200
        })
      }, 500) // 模拟网络延迟
    })
  }

  // 生产环境使用真实API
  return http({
    url: '/user/message/list',
    method: 'GET',
    params
  })
}

// 获取用户消息详情
export const getUserMessageDetail = (msgId: number): Promise<ApiResponse<Message | null>> => {
  // 开发环境使用模拟数据
  if (useMockData) {
    return new Promise(resolve => {
      setTimeout(() => {
        const message = getMockMessageDetail(msgId)
        if (message) {
          resolve({
            code: 200,
            message: 'success',
            data: message,
            status: 200
          })
        } else {
          resolve({
            code: 404,
            message: '消息不存在',
            data: null,
            status: 404
          })
        }
      }, 500) // 模拟网络延迟
    })
  }

  // 生产环境使用真实API
  return http({
    url: `/user/message/detail/${msgId}`,
    method: 'GET'
  })
}

// 标记用户单个消息为已读
export const readUserMessage = (msgId: number): Promise<ApiResponse<any>> => {
  // 开发环境使用模拟数据
  if (useMockData) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve({
          code: 200,
          message: '标记成功',
          data: null,
          status: 200
        })
      }, 300) // 模拟网络延迟
    })
  }

  // 生产环境使用真实API
  return http({
    url: `/user/message/read/${msgId}`,
    method: 'POST'
  })
}

// 标记用户所有消息为已读
export const readAllUserMessages = (): Promise<ApiResponse<any>> => {
  // 开发环境使用模拟数据
  if (useMockData) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve({
          code: 200,
          message: '全部标记成功',
          data: null,
          status: 200
        })
      }, 300) // 模拟网络延迟
    })
  }

  // 生产环境使用真实API
  return http({
    url: '/user/message/readAll',
    method: 'POST'
  })
}

// 删除用户消息
export const deleteUserMessage = (msgId: number): Promise<ApiResponse<any>> => {
  // 开发环境使用模拟数据
  if (useMockData) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve({
          code: 200,
          message: '删除成功',
          data: null,
          status: 200
        })
      }, 300) // 模拟网络延迟
    })
  }

  // 生产环境使用真实API
  return http({
    url: `/user/message/delete/${msgId}`,
    method: 'DELETE'
  })
}
